import unittest,requests,pymysql


class A3mall3(unittest.TestCase):
    def setUp(self):
        print('开始')
        self.driver = pymysql.connect(host='10.59.9.14',
                                      user='root',
                                      password='yidong666',
                                      database='a3mall')
        self.cursor = self.driver.cursor()

    def tearDown(self):
        self.cursor.close()
        self.driver.close()
        print('结束')

    id = None
    dy1 = None
    def test_01(self):
        self.cursor.execute('select id,store_nums from mall_goods where title="测试机003"')
        aa = self.cursor.fetchall()
        print(aa[0][1])
        A3mall3.id = aa[0][0]
        A3mall3.dy1 = aa[0][1]

    # 登录
    token = None
    def test_02(self):
        print(A3mall3.dy1)
        r2 = requests.post(url='http://10.59.9.14:7003/api/public/login',
                           json={"username":'18262840380', "password":'ll050821'})
        print(r2.json())
        A3mall3.token = r2.json()['data']['token']

    # 购物下单
    order_id = None
    def test_03(self):
        r3 = requests.post(url='http://10.59.9.14:7003/api/order/create',
                           headers={'Auth-token':'Bearer '+A3mall3.token},
                           json={"id":str(A3mall3.id),"type":"buy","address_id":59,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":f"http://10.59.9.14:7003/wap/pages/cart/confirm?id={A3mall3.id}&sku_id=&num=1&type=buy","sku_id":"","num":"1"})
        print(r3.json())
        if r3.json()['info']=='ok':
            num1 = A3mall3.dy1
            A3mall3.test_01(self)
            num2 = A3mall3.dy1
            # 购买商品后，数据库商品数量减一
            self.assertEqual(num1-1,num2)
